package cn.com.ry.framework.mongodb.adatper.util;

import com.alibaba.fastjson.serializer.ValueFilter;
import org.bson.types.ObjectId;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class ValueFilterTool {

    public static ValueFilter getValueFilter() {
        return new ValueFilter() {
            @Override
            public Object process(Object obj, String s, Object v) {
                if (v instanceof ObjectId) {
                    return v.toString();
                } else if (v instanceof Date) {
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(v);
                }
                return v;
            }
        };
    }

    /**
     * engine中的实体id对应数据库中的_id
     *
     * @param engineMap
     * @return
     */
    public static void getMongoDBMap(Map engineMap) {
        if (engineMap != null) {
            String id = (String) engineMap.remove("id");
            if (engineMap.get("_id") == null) {
                engineMap.put("_id", id);
            }
        }
    }
}
